package com.chenyue.cm.moudules.system.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chenyue.cm.base.bean.PageVo;
import com.chenyue.cm.base.service.BaseServiceImpl;
import com.chenyue.cm.base.bean.Pageable;
import com.chenyue.cm.common.utils.StringUtils;
import com.chenyue.cm.moudules.system.service.SysLogService;
import com.chenyue.cm.system.dao.SysLogMapper;
import com.chenyue.cm.system.domain.SysLog;
import com.chenyue.cm.system.domain.SysLogExample;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhaojie
 * @Title: SysLogServiceImpl
 * @ProjectName lc_master
 * @Description: TODO
 * @date 2018/11/2015:15
 */
@Service("sysLogService")
public class SysLogServiceImpl extends BaseServiceImpl<SysLog> implements SysLogService {

    private final SysLogMapper sysLogMapper;

    public SysLogServiceImpl(@Qualifier("sysLogMapper") SysLogMapper sysLogMapper) {
        this.sysLogMapper = sysLogMapper;
    }

    @Override
    @Resource(name = "sysLogMapper")
    public void setBaseDao(BaseMapper<SysLog> baseMapper) {
    	this.baseDao = baseMapper;
    }

    @Override
    public void saveLog(SysLog sysLog) {
        this.sysLogMapper.insert(sysLog);
    }

    @Override
    public PageVo<SysLog> querySysLogPageByUser(Pageable pageable, SysLog sysLog) {
        PageHelper.startPage(pageable.getPageIndex(), pageable.getPageSize());
        SysLogExample example = new SysLogExample();
        SysLogExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(sysLog.getUsername())) {
            criteria.andUsernameEqualTo(sysLog.getUsername());
        }
        if (StringUtils.isNotEmpty(sysLog.getOperation())) {
            criteria.andOperationEqualTo(sysLog.getOperation());
        }
        List<SysLog> resultList = this.sysLogMapper.selectByExample(example);
        PageVo<SysLog> sysLogPageVo = new PageVo<>(resultList, getBaseDao().selectList(null).size());
        return sysLogPageVo;
    }
}
